import { defineStore } from 'pinia'
import { getBannerList, getGridList, getSeckilllist, getGoodsList } from '@/api/home'
export const useHomeStore = defineStore('useHomeStore', {
  state() {
    return {
      bannerList: [],//轮播图
      gridList: [],//宫格数据
      seckilllist: [],//秒杀列表
      goodsList: [], //商品列表数据
    }
  },
  //   请求
  actions: {
    //发送请求,获取轮播图图片的数据
    async getBannerListHandler() {
      const result = await getBannerList();
      if ((result as any).code == 200) {
        this.bannerList = result.data

      }
    },

    // 宫格数据
    async getGridListHandler() {
      const result = await getGridList();
      (this as any).gridList = result

    },
    //发送请求,获取秒杀数据
    async getSeckilllistHandler(data: { count: number, limitNum: number }) {

      const result = await getSeckilllist(data);
      if ((result as any).code == 200) {
        this.seckilllist = result.data

      }
    },

    //发送请求，获取商品详情数据
    async getGoodsListHandler(data: { count: number, limitNum: number }) {
      const result = await getGoodsList(data);
      if ((result as any).code == 200) {
        //判断,如果获取到的数据是一个空的,那么我们就不需要再次获取数据了
        if(result.data.length == 0){
            return Promise.reject("不要获取了")
        }
        //将数据交给pinia
        //concat 合并数组
        this.goodsList = this.goodsList.concat(result.data)
        
      }
    },
  }


})
